package iosr11.example;

import iosr11.Service.MarkService;
import iosr11.Service.ServiceCreator;
import iosr11.Service.ServiceLocator;
import iosr11.Service.ServiceLocatorImpl;
import iosr11.Service.SortResult;
import iosr11.Service.StudentService;
import iosr11.Service.TeacherService;
import iosr11.data.Mark;
import iosr11.data.MarkKind;
import iosr11.data.Student;
import iosr11.data.Teacher;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class StudentServiceMain {
	
	
	
	
	public static void main(String[] args) {
		ServiceCreator.createService();
		ServiceLocator locator = ServiceLocatorImpl.getInstance();
		
		StudentService service = (StudentService) locator.getService(Student.class);
		MarkService servMark = (MarkService) locator.getService(Mark.class);
		TeacherService servTeacher = (TeacherService) locator.getService(Teacher.class);
		
		// dodawanie studenta
		/*
		Student student = new Student();
		
		student.setName("Izabela Górska");
		student.setEmail("iza.g@gmail.com");
		student.setIndexNumber(654345);
		System.out.println(service.createStudent(student.getName(), student.getEmail(), student.getIndexNumber()));
		//System.out.println(service.getStudent("ASDFG.ASD@dcfgfh.ij"));
		// */
		
		System.out.println(service.getAllStudents());
		
		// lista kontaktow
		//List<Student> lista = service.getAllStudents();
		//SortResult.sortStudents(lista);
		//System.out.println(lista);
		// */
		/*
		List<Student> lista = service.getAllStudents();
		List<Student> newList = new ArrayList<Student>();
		for(Student st : lista){
			if(st.getGrades() == null)
				newList.add(st);
			else if (st.getGrades().size() < 1)
				newList.add(st);
		}
		//System.out.println(newList);
		Teacher t = servTeacher.getTeacher("jacek.krol@gmail.com");
		//System.out.println(t);
		addMarks(servMark, newList, t, 1);
		
		
		
		*/
		
		//service.getStudent("test_dwa@gmail.com");
		/*
		//usuwanie studenta
		Student s = service.getStudent("test.zgrupa@gmail.com");
		System.out.println("usuwanie");
		System.out.println(s);
		System.out.println(service.deleteStudent(s.getEmail()));
		
		// */
		
		/*
		//Zmiana danych
		Student s = service.getStudent("ASDFG.ASD@dcfgfh.ij");
		System.out.println("usuwanie");
		System.out.println(s);
		s.setName("Nowy kowalski jan");
		s.setEmail("jkjk@gmail.com");
		s.setIndexNumber(654321);
		System.out.println(service.updateStudent(s));
		// */
	}
	
	private static void addMarks(MarkService servMark, List<Student> lista, Teacher te, int numberOfGrades)
	{
		for (Student st : lista){
			
					
			Random randomGenerator2 = new Random();
			for (int i = 1; i <= 3; ++i){
				int randomInt2 = randomGenerator2.nextInt(100);
				//System.out.println("Generated2 : " + ((randomInt2%2)) );
				Random randomGenerator = new Random();
				for (int idx = 1; idx <= numberOfGrades; ++idx){
					int randomInt = randomGenerator.nextInt(100);
					randomInt = ((randomInt%4)+2);
					System.out.println("Generated : " + randomInt );			
					Mark mark = new Mark();
					Date date = new Date();
					date.setTime(Calendar.getInstance().getTimeInMillis());
					mark.setDate(date);
					mark.setTeacher(te);
					mark.setStudent(st);
					mark.setGrade((float) randomInt);
					
					if (randomInt2==0)
						mark.setKind(MarkKind.CREDIT);
					else if (randomInt2==1)
						mark.setKind(MarkKind.EXAM);
					else 
						mark.setKind(MarkKind.PROJECT);
					mark.setPoints(mark.getGrade()*5f);
					//System.out.println("===============Dodaje dla: "+st.getName());
					System.out.println(servMark.createMark(st, te, mark));
				}
			}
	}
	}
}
